<template>
  <div class="app-container">
    <div
      style="
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 0px 20px 10px 0px;
      "
    >
      <el-button
        type="primary"
        plain
        icon="Plus"
        @click="handleAdd"
        v-hasPermi="['system:protocol:add']"
        style="margin-left: 20px"
      >
        新增系统文章
      </el-button>

      <div>
        <el-select
          v-model="queryParams.type"
          placeholder="请选择"
          style="width: 200px"
          @change="handleQuery"
        >
          <el-option
            v-for="item in classifyList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>

        <el-input
          v-model="queryParams.title"
          style="max-width: 200px; margin-left: 10px"
          placeholder="请输入文章标题"
          @change="handleQuery"
        >
          <template #append>
            <el-button :icon="Search" />
          </template>
        </el-input>
      </div>
    </div>
    <el-table v-loading="loading" :data="postList">
      <el-table-column align="center" prop="title" label="文章标题" />
      <el-table-column align="center" prop="type" label="文章类型">
        <template #default="scope">
          {{
            classifyList.filter((item) => scope.row.type == item.id)[0] &&
            classifyList.filter((item) => scope.row.type == item.id)[0].name
          }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="content" label="文章内容" />
      <el-table-column align="center" prop="isEnable" label="是否启用">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isEnable"
            @change="onChangeIsEnable(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createBy" label="创建人" />
      <el-table-column align="center" prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:protocol:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:protocol:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改系统文章对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form
        ref="protocolRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入文章标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择">
                <el-option
                  v-for="item in classifyList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否启用">
              <el-radio-group v-model="form.isEnable">
                <el-radio label="启用" :value="true"></el-radio>
                <el-radio label="不启用" :value="false"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <editor
                v-model="form.content"
                :min-height="192"
                v-if="isEditor"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Classification">
import { Search } from "@element-plus/icons-vue";
import { ref } from "vue";
import {
  getAgreementApi,
  addAgreementApi,
  getAgreementByIdApi,
  updateAgreementApi,
  delAgreementApi,
  updateEnableApi,
  getClassifyApi,
} from "@/api/system/protocol";

const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const total = ref(0);
const open = ref(false);
const title = ref("");
const classifyList = ref([]);
const isEditor = ref(false);
const data = reactive({
  form: {
    title: "",
    type: "",
    isEnable: "",
    content: "",
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: "",
    type: "",
  },
  rules: {
    title: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询系统文章列表 */
function getList() {
  loading.value = true;
  getAgreementApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/**
 * 获取系统文章分类列表
 */
const getClassify = () => {
  getClassifyApi().then((res) => {
    if (res.code == 200) {
      console.log("res", res.data);
      classifyList.value = res.data;
    }
  });
};
getClassify();

/**
 * 改变启用、禁用状态
 * @param row
 */
const onChangeIsEnable = (row) => {
  updateEnableApi({
    id: row.id,
    status: row.isEnable,
  }).then((res) => {
    proxy.$modal.msgSuccess("修改成功");
    getList();
  });
};

/**
 * 按名字搜索系统文章
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/**
 * 重置
 */
const rest = () => {
  form.value = {
    title: "",
    type: "",
    isEnable: "",
  };
  form.value.content = "";
  proxy.resetForm("protocolRef");
};

/** 新增按钮操作 */
function handleAdd() {
  rest();
  isEditor.value = true;
  open.value = true;
  title.value = "新增系统文章";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  rest();
  const postId = row.id;
  getAgreementByIdApi(postId).then((response) => {
    form.value = response.data;
    form.value.content = response.data.content;
    isEditor.value = true;
    open.value = true;
    title.value = "修改系统文章";
  });
}

/** 取消按钮 */
function cancel() {
  rest();
  isEditor.value = false;
  open.value = false;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["protocolRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateAgreementApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          isEditor.value = false;
          open.value = false;
          getList();
        });
      } else {
        addAgreementApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          isEditor.value = false;
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id;
  proxy.$modal
    .confirm(`是否确认删除名称为${row.title}的系统文章数据项？`)
    .then(function () {
      return delAgreementApi(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style scoped>
:deep(.el-form-item) {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
</style>
